package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 新唐人
 * @author KK
 *
 */
public class CreadersNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://bbs.creaders.net/politics/bbsviewer.php\\?blog_id=[0-9]*&trd_id=[0-9]*",
		"http://blog.creaders.net/.*/user_blog_diary.php\\?did=[0-9]*",
		"http://bbs.creaders.net/.*/bbsviewer.php\\?.*trd_id=[0-9]*.*",
		"http://.*.creaders.net/.*/newsViewer.php\\?.*&id=[0-9]*.*",
		"http://.*.creaders.net/.*/[0-9]*/[0-9]*/[0-9]*/[0-9]*.html",
		"http://bbs.creaders.net/[a-z]*/bbsviewer.php\\?trd_id=[0-9]*&blog_id=[0-9]*",
	};


	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if (url.startsWith("http://bbs.creaders.net")){
			title =doc.select("#trd_subject").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("#trd_content  p"));
			date=DateUtils.matchDate(doc.select(".trd_info").text());
			author =doc.select("a.trd_info[target]").text();
		}

		if (url.startsWith("http://blog.creaders.net")){
			title=doc.select(".diary_title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select(".diary_content"));
			date=DateUtils.matchDate(doc.select(".diary_release_date").text());
			author=doc.select(".user_info1").text();
		}

		if (url.startsWith("http://news.creaders.net")){
			title=doc.select("#newsTitle").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("#newsContent"));
			date=DateUtils.matchDate(doc.select("#subTitle").text());
		}

		if (url.startsWith("http://digest.creaders.net/articleViewer.php?")){
			url=StringUtils.substringBetween(htmltxt, "str=\"","\";");
			if (StringUtils.isNotBlank(url)){
				urlMeta=CrawlHTML.responseToURL(url);
				return parserHtml(urlMeta);
			}
		}
		
		if(StringUtils.isBlank(title))
		{
			title = doc.select("span.STYLE3").text();
		}
		
		if(date==null)
		{
			date = DateUtils.matchDate(doc.select("span.STYLE4").text());
		}
		
		if(StringUtils.isBlank(content))
		{
			content = doc.select("div#newsContent").text();
		}
		
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
